import time
import random


HANGMANPICS = ['''
+---+
|   |
    |
    |
    |
    |
======
''', '''
+---+
|   |
O   |
    |
    |
    |
======
''', '''
+---+
|   |
O   |
|   |
    |
    |
======
''', '''
+---+
|   |
O   |
/|  |
    |
    |
======
''', '''
+---+
|   |
O   |
/|\ |
    |
    |
======
''', '''
+---+
|   |
O   |
/|\ |
/   |
    |
======
''', '''
+---+
|   |
O   |
/|\ |
/\  |
    |
======
''']

# introduce the background.


def intro():
    print('Welcom to Hangman game !')
    # time.sleep(2)
    print(
        'if your can\'t guss all the letters in the 6 step ,you will be a Hangman! ')
    # time.sleep(2)
    print('Good Luck!')
    # time.sleep(2)
    print()
    return('Firstly please let your friend input a word,but you can not see !')
    # word = input()
    # return word

# 选定单词,返回随机单词


def randomWord():
    words = ['banana', 'eerr','aabbccddd']
    return random.choice(words)

# replaceLetter
'''
随机用_替换单词种60%的字母,返回替换后的字符
'''


def replaceLetter(word):
    a = word
    b = set([random.randint(0, len(a) - 1) for x in range(int(len(a) * 0.6))])
    lengh = len(b)
    letter = set()
    for x in b:
        if {word[b] for }
        letter.add(a[x])
        a = a.replace(a[x], '_', 1)
        c = a.replace('_', ' _ ')
    # print(x)
    # print(a)
    return (a, lengh, c, letter)


# rightLetter
'''
如果猜对里面的字母,打印包含该字母的单词(猜对的是哪个字母)
'''


def rightLetter(index, underlineWord, letter):
    underlineWord = underlineWord[:index] + letter + underlineWord[1 + index:]
    return underlineWord

# wrongLetter
'''
如果猜错,打印对应错误数目的图形(出错的第几次)
'''


def wrongLetter(erroNum):
    return (HANGMANPICS[erroNum], 6 - erroNum)

# 输入字母(),返回字母 inputLetter


def inputLetter():
    letter = input()
    return letter

# 重复字母处理,


def findIndex(word, letter):
    n = 0
    cc = set()
    for i in word:
        if i == letter:
            cc.add(word.index(i, n))
        n = n + 1
    return cc

# 主程序
print('H A N G M A N' + HANGMANPICS[6])
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
    intro()
    gussWord = randomWord()
    underlineWord = replaceLetter(gussWord)[0]
    print(underlineWord)
    erroNum = 0
    letterIndex = 0
    print(wrongLetter(erroNum)[0])

    while underlineWord != gussWord:
        print('Please input your word')
        letter = inputLetter()
        doubleIndex = findIndex(gussWord, letter)
        if letter in [gussWord[i] for i in doubleIndex]:
            letterIndex = gussWord.index(letter)
            underlineWord = rightLetter(letterIndex, underlineWord, letter)
            # letterIndex = letterIndex + 1
            print(underlineWord)
            if underlineWord == gussWord:
                print(
                    'Congratuation! you are alive ...' + ' \n It\'s ' + '**' + gussWord + '**')
            else:
                print('Good Job,go on guy!')
        else:
            erroNum = erroNum + 1
            print(wrongLetter(erroNum)[0])
            print(underlineWord)
            print(
                'you only have ' + str(wrongLetter(erroNum)[1]) + ' chances !,\n Try again!')
            if erroNum == 6:
                print('Sorry you have die ! Game Over !')
                print()
                print('The right world is ' + '**' + gussWord + '**')
                break
    print()
    print('Do you want to play again ?')
    playAgain = input()


# print(replaceLetter(intro()))
